package com.situ.util.controller;

import com.situ.util.entity.SysUser;
import com.situ.util.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import com.situ.util.service.CstServiceService;
import com.situ.util.entity.CstService;
import com.situ.util.*;

import java.beans.PropertyEditor;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * CstService的路由接口服务
 * 
 * @author 
 *
 */
@RestController
public class CstServiceController {

	/** CstServiceService服务 */
	@Autowired
	private CstServiceService cstServiceService;
	@Autowired
	private SysUserService sysUserService;

	/**
	 * 查询所有CstService数据的方法
	 * @param value
	 * @return
	 */
	@RequestMapping(value = "queryCstService", produces = {"application/json;charset=UTF-8"})
	public ResponseData find(CstService value) {

		return ResponseData.ok().putDataValue("list",cstServiceService.find(value));
	}
	
	/**
	 * 通过id查询CstService数据的方法
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "queryCstServiceById/{id}", produces = {"application/json;charset=UTF-8"})
	public ResponseData findOne(@PathVariable(name="id") Integer id) {
		 return ResponseData.ok().putDataValue("obj",cstServiceService.findOne(id));
	}
	
	/**
	 * 插入CstService属性不为空的数据方法
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "saveCstService")
	public ResponseData save(CstService value) {
		return ResponseData.ok().putDataValue("obj", cstServiceService.saveNotNull(value));
	}
	
	/**
	 * 更新CstService属性不为空的数据方法
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "updateCstService", produces = {"application/json;charset=UTF-8"})
	public ResponseData update(CstService value) {
		return ResponseData.ok().putDataValue("obj",cstServiceService.updateNotNullById(value));
	}

	/**
	 * 通过id删除CstService数据方法
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "delCstServiceById/{id}", produces = {"application/json;charset=UTF-8"})
	public ResponseData delete(@PathVariable(name="id") Integer id) {
		return ResponseData.ok().putDataValue("obj",cstServiceService.deleteById(id));
	}


	//添加
	@RequestMapping("/addcs")
	public ResponseData addSal(CstService cs){

		cstServiceService.addServerFw(cs);

		return  ResponseData.ok();
	}


	//删除
	@RequestMapping("/delCstServiceById/{id}")
	public ResponseData delCstServiceById(@PathVariable Integer id){

		cstServiceService.deleteCstServiceById2(id);

		return  ResponseData.ok();
	}
	//根据id查询
	@RequestMapping("/queryCstSerById/{id}")
	public ResponseData queryCstSerById(@PathVariable Integer id){

		CstService cs= cstServiceService.selectCstSerById(id);

		return  ResponseData.ok().putDataValue("cs", cs);
	}
	//分配
	@RequestMapping("/updateSdtById")
	public ResponseData updateSdtById(CstService c){

		SysUser user = sysUserService.findOne(c.getSvr_due_id());
		c.setSvr_due_to(user.getUsr_name());
		c.setSvr_status("已分配");
		cstServiceService.updatesdt(c);

		return  ResponseData.ok();
	}

	//处理
	@RequestMapping("/updateCs")
	public ResponseData updateCs(CstService c){

		c.setSvr_status("已处理");
		cstServiceService.updateCs(c);

		return  ResponseData.ok();
	}

	//反馈
	@RequestMapping("/updateFk")
	public ResponseData updateFk(CstService c){

		if(c.getSvr_satisfy()>=3) {
			c.setSvr_status("已归档");
			cstServiceService.updateFk(c);
		}else {
			c.setSvr_status("已分配");
			c.setSvr_deal(null);
			c.setSvr_deal_by(null);
			c.setSvr_deal_id(null);
			c.setSvr_deal_date(null);
			c.setSvr_result(null);
			c.setSvr_satisfy(null);
			cstServiceService.updateXxy3(c);
		}

		return  ResponseData.ok();
	}

	//时间转换器
	@InitBinder  //
	public void dateconvert(WebDataBinder bind){
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		PropertyEditor n=new CustomDateEditor(format, true);
		bind.registerCustomEditor(Date.class, n);
	}

}
